<?php
/**
 * Itc central cache resource.
 *
 * Change log
 *
 * @category   Itc
 * @package    Itc_Application
 * @author     Dan Yasnyuk <dan@techs.com.ua>
 * @version    $Rev: 2043 $ $Date: 2011/07/12 16:41:41 $ $Author: alex $
 * @date       2009-12-25
 */
class Itc_Application_Resource_Cache extends Zend_Application_Resource_ResourceAbstract
{
    /**
     * Default registry key
     */
    const DEFAULT_REGISTRY_KEY = 'Itc_Cache';

    /**
     * Cache instance
     *
     * @var Zend_Cache
     */
    protected $cache = null;

    /**
     * Inititalize cache resource
     *
     * @return Zend_Cache
     */
    public function init()
    {
        return $this->getCache();
    }

    /**
     * Return cache instance
     *
     * @return Zend_Cache
     */
    public function getCache()
    {
        if (null === $this->cache) {
            $options = $this->getOptions();
            // we can use full page caching only for non-logged users
            if (!Zend_Auth::getInstance()->hasIdentity()) {

                // default page cache frontend parameters
                $pcFrontParams = $options['frontend']['params'];

                if (!empty($options['frontend']['regexps'])) {
                    $pcFrontParams['regexps'] = array();

                    foreach ($options['frontend']['regexps'] as $option) {
                        $pcFrontParams['regexps'][$option['exp']] = $option['params'];
                    }
                }

                // force specific page caching
                $pageCache = Zend_Cache::factory(
                    'Page',
                    $options['backend']['adapter'],
                    $pcFrontParams,
                    $options['backend']['params']
                );

                $pageCache->start();

            }

            /// create cache instance
            $this->cache = Zend_Cache::factory(
                $options['frontend']['adapter'],
                $options['backend']['adapter'],
                $options['frontend']['params'],
                $options['backend']['params']
            );

            /// use as default database metadata cache
            if (isset($options['isDefaultMetadataCache']) && true === (bool) $options['isDefaultMetadataCache']) {
                Zend_Db_Table_Abstract::setDefaultMetadataCache($this->cache);
            }

            /// use as default translate cache
            if (isset($options['isDefaultTranslateCache']) && true === (bool) $options['isDefaultTranslateCache']) {
                Zend_Translate::setCache($this->cache);
            }

            /// use as default locale cache
            if (isset($options['isDefaultLocaleCache']) && true === (bool) $options['isDefaultLocaleCache']) {
                Zend_Locale::setCache($this->cache);
            }

            /// add to registry
            $key = (isset($options['registry_key']) && !is_numeric($options['registry_key'])) ? $options['registry_key'] : self::DEFAULT_REGISTRY_KEY;

            Zend_Registry::set($key, $this->cache);
        }

        return $this->cache;
    }
}
